package jwebtester.business.services.impl;

import javax.faces.context.FacesContext;

import jwebtester.business.services.LoginService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

@Service
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public class LoginServiceImpl implements LoginService  {

	@Autowired
	@Qualifier("customAuthenticationManager")
	private transient AuthenticationManager authManager;
	
	@Override
	public Authentication authenticate(final String userName, final String password) {
		final Authentication request = new UsernamePasswordAuthenticationToken(
				userName, password);

		return authManager.authenticate(request);
	}

	@Override
	public void logout() {
		FacesContext.getCurrentInstance().getExternalContext()
				.invalidateSession();
	}
}
